package a10_动态规划;

/**
 * <p>
 * a07_不同路径2复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/3/26
 */
public class a07_不同路径2复习3 {

    //dp[i][j]：从0,0到i,j有dp[i][j]种方法

    //递推公式：
    //if(obstacleGrid[i][j] == 0){
    //  dp[i][j] = dp[i][j-1] + dp[i-1][j]
    //}

    //初始化：
    //if(obstacleGrid[i][0] == 0){
    //   dp[i][0] = 1;
    //}
    //if(obstacleGrid[0][j] == 0){
    //    dp[0][j] = 1;
    //}
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int rows = obstacleGrid.length;
        int cols = obstacleGrid[0].length;
        int[][] dp = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            if(obstacleGrid[i][0] == 0){
                dp[i][0] = 1;
            }else {
                break;
            }
        }

        for (int j = 0; j < cols; j++) {
            if(obstacleGrid[0][j] == 0){
                dp[0][j] = 1;
            }else {
                break;
            }
        }

        for (int i = 1; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if(obstacleGrid[i][j] == 0){
                    dp[i][j] = dp[i][j-1] + dp[i-1][j];
                }
            }
        }

        return dp[rows-1][cols-1];


    }
}
